package com.sgffsg.learn.javalib.mergetest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lmk
 * @date: 2024/4/8
 * @desc: 合并数组
 */

/*
 * 两个有序数组合并为一个
 * 如{1,1,3} {2,4}合并为{1,1,2,3,4}
 * 要求时间复杂度不高于O(M + N)  (假设两个数组长度分别为M，N)，
 * 空间复杂度也是: O(M + N)
 **/
public class MergeObjectTest2 {

    public static void main(String[] args) {
        test(new int[]{1, 3, 5}, new int[]{2, 4, 6}, new int[]{1, 2, 3, 4, 5, 6});
        test(new int[]{1, 1, 3}, new int[]{2, 4}, new int[]{1, 1, 2, 3, 4});
        test(new int[]{1}, new int[]{2}, new int[]{1, 2});
        test(new int[]{1}, new int[]{}, new int[]{1});
        test(new int[]{}, new int[]{1}, new int[]{1});

    }


    public static void test(int a[], int b[], int c[]) {
//        int r[] = merge(a, b);
//        if (r == null || r.length != c.length) {
//            throw new IllegalStateException("r == null or inconsistent length error");
//        }
//        for (int i = 0; i < r.length; i++) {
//            if (c[i] != r[i]) {
//                throw new IllegalStateException("char inconsistent");
//            }
//        }

//        Collections.sort()
        System.out.println("OK");
        Integer integer = 2;
    }

    private static <T extends Comparable> List<T> merge(List<T> a, List<T> b) {
        List<T> result = new ArrayList<T>();
        int indexA = 0;
        int indexB = 0;
        int indexNew = 0;

        int aSize = a.size();
        int bSize = b.size();
        if (aSize == 0) {
            result.addAll(b);
            return result;
        }
        if (bSize == 0) {
            result.addAll(a);
            return result;
        }
        while (indexA < aSize || indexB < bSize) {
//            System.out.println("indexA = " + indexA + ", indexB = " + indexB);
//            if (indexA == aSize){
//                result.add(b.get(indexB));
//                indexB++;
//            } else if (indexB == bSize){
//                result.add(a.get(indexA));
//                indexA++;
//            } else if (a.get(indexA).compare(b.get(indexB))){
//                result.add(a.get(indexA));
//                indexA++;
//            } else {
//                result.add(b.get(indexB));
//                indexB++;
//            }


        }
        printArray(result);
        return result;
    }

    private static <T> void  printArray(List<T> result){
        int size = result.size();
        for (int i = 0; i < size; i++){
            System.out.print(result.get(i).toString());
        }
    }

}
